package com.icode.web.controller.house;

import com.icode.core.dto.house.EquipmentDTO;
import com.icode.core.model.house.EquipmentType;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SunKai
 * Date: 14-11-5
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/house/special")
public class SpecialController {
    @Autowired
     private HouseService houseService;

    @RequestMapping(value = "form",method = RequestMethod.GET)
    public ModelAndView showForm(){
            return new ModelAndView("back/houseBase/special","equipment", new EquipmentDTO());
       }

    @RequestMapping(value = "form",method = RequestMethod.POST)
    public String submitForm(EquipmentDTO equipmentDTO){
           houseService.saveOrUpdateEquipment(equipmentDTO, EquipmentType.SPECIAL);
           return "redirect:/house/special/list";
}

    @RequestMapping(value="list",method = RequestMethod.GET)
    public String showEquipments(Model model) throws Exception{
        List<EquipmentDTO> equipmentDTOs= houseService.getEquipmentDTOs(EquipmentType.SPECIAL);
        model.addAttribute("list",equipmentDTOs);
        return "back/houseBase/specialOverview";
    }

    @RequestMapping(value="update/{guid}",method=RequestMethod.GET)
    public String update(@PathVariable String guid,Model model){
        EquipmentDTO equipmentDTO=houseService.getEquipmentByGuid(guid);
        model.addAttribute("equipment",equipmentDTO) ;
        return "back/houseBase/special" ;
    }

    @RequestMapping(value="delete/{guid}",method=RequestMethod.GET)
    public String delete(@PathVariable String guid){
        houseService.deleteEquipmentByGuid(guid);
        return "redirect:/house/special/list" ;
    }
}
